package de.keychain

import org.codehaus.groovy.grails.plugins.codecs.SHA256Codec;

import grails.test.mixin.TestFor
import spock.lang.Specification

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Password)
class PasswordSpec extends Specification {

	def passwordString = "abc123"
	
    def setup() {
		mockDomain(Password)
	}

    void "test if password is encoded when needed"() {
		
		given: "a valid password instance"
			def password = new Password(password:passwordString)
			
		when: "password encodePassword is triggered and is saved"
			
			password.encodePassword()
			assert(password.save())
			
		then: "password should be SHA-256 encoded"
			
			assert(password.password == SHA256Codec.encode(passwordString))		
    }
}
